package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.logistics.pojo.dto.LogisticsSaveDTO;
import com.xbongbong.pro.logistics.pojo.vo.LogisticsVO;
import com.xbongbong.saas.service.LogisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;


/**
 * 物流设置页数据获取
 * @author wujian
 * @date 2020/12/25 15:52
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/logisticsSet")
public class LogisticsSetController {
    @Resource
    private LogisticsService logisticsService;

    private static final Logger LOG = LoggerFactory.getLogger(LogisticsSetController.class);


    @RequestMapping(value = "/getLogistics", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getLogistics(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<LogisticsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LogisticsVO logisticsVO = logisticsService.getLogistics(baseDTO);
                response = new XbbResponse<>(logisticsVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/saveLogistics", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveLogistics(@RequestBody @Valid LogisticsSaveDTO logisticsSaveDTO, BindingResult br) {
        XbbResponse<LogisticsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LogisticsVO logisticsVO = logisticsService.saveLogistics(logisticsSaveDTO);
                response = new XbbResponse<>(logisticsVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
